﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using vngoodlife.Util;
using vngoodlife.Singleton;
using vngoodlife.DTO;

namespace vngoodlife.Models
{
    public class Comment_Validation
    {

        //[System.ComponentModel.DataAnnotations.MetadataType(typeof(vngoodlife.Models.Comment_Validation))]

        [Required]
        public virtual int CommentTypeID { get; set; }

        [Required(ErrorMessage = "*")]
        public virtual string CommentContent { get; set; }

        [Required]
        public virtual string GuestName { get; set; }

        [Required]
        [RegularExpression(ConstantClass.testEmailRegular_1, ErrorMessage = "Email không hợp lệ!")]
        public virtual string GuestEmail { get; set; }

    }


    public class CommentModels
    {

        /// <summary>
        /// Get Comment Type
        /// </summary>
        /// <returns></returns>
        internal IQueryable<CommentType> GetCommentType()
        {
            return DataSingleton.Instance.GetLinQContext().CommentTypes;
        }

        /// <summary>
        /// Get comment list filter by comment type
        /// </summary>
        /// <param name="commentTypeID"></param>
        /// <returns></returns>
        internal IQueryable<Comment> GetCommentList(int commentTypeID, int amountOfComment, bool isBadOrPosted)
        {
            if (commentTypeID == 1)
            {
                return DataSingleton.Instance.GetLinQContext().Comments.Where(m => m.CommentTypeID == commentTypeID && m.IsBad == isBadOrPosted)
                    .OrderByDescending(m => m.CommentID).Take(amountOfComment);
            }
            else
            {
                return DataSingleton.Instance.GetLinQContext().Comments.Where(m => m.CommentTypeID == commentTypeID && m.IsPosted == isBadOrPosted)
                    .OrderByDescending(m => m.CommentID).Take(amountOfComment);
            }
        }

        /// <summary>
        /// Change bad status of member comment
        /// </summary>
        /// <param name="commentID"></param>
        /// <param name="isBad"></param>
        internal void ChangeIsBad(int commentID, string memberName)
        {
            try
            {
                Comment tempComment = DataSingleton.Instance.GetLinQContext().Comments.SingleOrDefault(c => c.CommentID == commentID);
                tempComment.IsBad = !tempComment.IsBad;
                tempComment.Admin = DataSingleton.Instance.GetLinQContext().Members.SingleOrDefault(m => m.MemberName == memberName);
                tempComment.UpdateDate = DateTime.Now;

                DataSingleton.Instance.GetLinQContext().Comments.Context.SubmitChanges();
            }
            catch (Exception)
            {
                DataSingleton.Instance.RefreshInstance();
            }
        }


        /// <summary>
        /// Change posted status of guest comment
        /// </summary>
        /// <param name="commentID"></param>
        /// <param name="isBad"></param>
        internal void ChangeIsPosted(int commentID, string memberName)
        {
            try
            {
                Comment tempComment = DataSingleton.Instance.GetLinQContext().Comments.SingleOrDefault(c => c.CommentID == commentID);
                tempComment.IsPosted = !tempComment.IsPosted;
                tempComment.Admin = DataSingleton.Instance.GetLinQContext().Members.SingleOrDefault(m => m.MemberName == memberName);
                tempComment.UpdateDate = DateTime.Now;

                DataSingleton.Instance.GetLinQContext().Comments.Context.SubmitChanges();
            }
            catch (Exception)
            {
                DataSingleton.Instance.RefreshInstance();
            }
        }


        /// <summary>
        /// Delete a comment
        /// </summary>
        /// <param name="commentID"></param>
        internal void DeleteComment(int commentID)
        {
            try
            {
                DataSingleton.Instance.GetLinQContext().Comments.DeleteOnSubmit(DataSingleton.Instance.GetLinQContext().Comments
                    .SingleOrDefault(c => c.CommentID == commentID));

                DataSingleton.Instance.GetLinQContext().Comments.Context.SubmitChanges();
            }
            catch (Exception)
            {
                DataSingleton.Instance.RefreshInstance();
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="commentID"></param>
        /// <returns></returns>
        internal Comment GetCommentDetails(int commentID)
        {
            return DataSingleton.Instance.GetLinQContext().Comments.SingleOrDefault(c => c.CommentID == commentID);
        }


        /// <summary>
        /// Insert comment into database by comment type
        /// </summary>
        /// <param name="guestName"></param>
        /// <param name="guestEmail"></param>
        /// <param name="commentContent"></param>
        internal void CreateComment(Comment comment, int commentTypeID, string memberName)
        {
            try
            {
                //Comment comment = new Comment();
                comment.CommentTypeID = commentTypeID;
                comment.MemberName = memberName;
                comment.CommentContent = GeneralClass.ReplaceSpecialCharacterForCK(comment.CommentContent);
                comment.GuestName = comment.GuestName == null ? null : comment.GuestName.Trim();
                comment.CreateDate = DateTime.Now;
                comment.UpdateDate = DateTime.Now;

                DataSingleton.Instance.GetLinQContext().Comments.InsertOnSubmit(comment);
                DataSingleton.Instance.GetLinQContext().Comments.Context.SubmitChanges();

            }
            catch (Exception)
            {
                DataSingleton.Instance.RefreshInstance();
            }
        }


        /// <summary>
        /// Get Total comment in a article
        /// </summary>
        /// <param name="articleID"></param>
        /// <returns></returns>
        internal int GetTotalCommentIn_Frontend(int articleID)
        {
            return DataSingleton.Instance.GetLinQContext().Comments.Where(c => c.ArticleID == articleID &&
                ((c.CommentTypeID == 1 && c.IsBad == false) || (c.CommentTypeID == 2 && c.IsPosted == true))).Count();
        }


        /// <summary>
        /// Get all available comment (not bad and allow to post)
        /// </summary>
        /// <param name="articleID"></param>
        /// <param name="currentPageIndex"></param>
        /// <param name="commentPageStep"></param>
        /// <returns></returns>
        internal IQueryable<Comment> GetCommentOfArticle_Frontend(int articleID, int currentPage, int commentPageStep)
        {
            int startRowIndex = (Convert.ToInt16(currentPage) - 1) * commentPageStep;

            return DataSingleton.Instance.GetLinQContext().Comments.Where(c => c.ArticleID == articleID &&
                (c.CommentTypeID == 1 || (c.CommentTypeID == 2 && c.IsPosted == true))).OrderByDescending(c => c.CommentID).Skip(startRowIndex)
                .Take(commentPageStep);
        }
    }
}